<?php

/*
 * 文件说明.
 * 
 * @author Kun Xu
 */

/**
 * Description of NewsController
 *
 * @author 坤
 */
class NewsController extends Controller
{
    public function actionIndex()
    {
        $model = new News('search');
        $model->unsetAttributes();
        if ($_GET['News']) {
            $model->attributes = $_GET['News'];
        }
        $this->render('index', array('model' => $model));
    }

    public function actionDelete()
    {
        if (Yii::app()->request->isPostRequest) {
            $id = $_GET['id'];
            $model = News::model()->findByPk($id);
            if ($model === null) {
                throw new CHttpException(404, "您要浏览的页面不存在，可能是已被删除或者URL地址错误。");
            } else {
                $model->delete();
            }
            if (!$_GET['ajax']) {
                $this->redirect('index');
            }
        } else {
            throw new CHttpException(400, '访问受限！');
        }
    }

    public function actionView($id)
    {
        $model = News::model()->findByPk($id);
        $this->render('view', array('model' => $model));
    }

    public function actionCreate()
    {
        $form = new Form("backend.views.news.formConfig");
        $form->model = new News( );
        if (isset($_POST['ajax']) && $_POST['ajax'] === "edit-form") {
            echo CActiveForm::validate($form->model);
            Yii::app()->end();
        }
        Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/ueditor/ueditor.config.js');
        Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/ueditor/ueditor.all.min.js');
        if ($form->submitted("btnsubmit")) {
            $images = array("logo");
            foreach ($images as $name) {
                $image = CUploadedFile::getinstance($form->model, $name);
                if ($image) {
                    $filename = time() . rand(1000, 9999) . "." . $image->getExtensionName();
                    $dir = "/upload/" . $name . "/" . date("Ym") . "/";
                    if (!file_exists(Yii::app()->basePath . "/.." . $dir)) {
                        FileHelper::mkdirs(Yii::app()->basePath . "/.." . $dir);
                    }
                    $full_filename = Yii::app()->basePath . "/.." . $dir . $filename;
                    if ($image->saveAs($full_filename)) {
                        $form->model->$name = $dir . $filename;
                    }
                }
            }
            if ($form->validate()) {
                $model = $form->model;
                if ($model->save(FALSE)) {
                    Yii::app()->user->setFlash("success", News::model()->modelName . ( "“" . $model . "”已成功添加！" ));
                    $this->redirect(array(
                        "view",
                        "id" => $model->id
                    ));
                }
            } else {
                foreach ($images as $name) {
                    @unlink(Yii::app()->basePath . "/.." . $form->model->$name);
                }
            }
        }
        $this->render("create", array(
            "form" => $form
        ));
    }

    public function actionUpdate($id)
    {
        $form = new Form("backend.views.news.formConfig");
        $form->model = News::model()->findByPk($id);
        if (isset($_POST['ajax']) && $_POST['ajax'] === "edit-form") {
            echo CActiveForm::validate($form->model);
            Yii::app()->end();
        }
        Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/ueditor/ueditor.config.js');
        Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/ueditor/ueditor.all.min.js');
        $images = array("logo");
        if ($form->submitted("btnsubmit")) {
            $filenames = array();
            foreach ($images as $name) {
                $image = CUploadedFile::getinstance($form->model, $name);
                if ($image) {
                    $filename = time() . rand(1000, 9999) . "." . $image->getExtensionName();
                    $dir = "/upload/" . $name . "/" . date("Ym") . "/";
                    if (!file_exists(Yii::app()->basePath . "/.." . $dir)) {
                        FileHelper::mkdirs(Yii::app()->basePath . "/.." . $dir);
                    }
                    $full_filename = Yii::app()->basePath . "/.." . $dir . $filename;
                    if ($image->saveAs($full_filename)) {
                        $filenames[] = $form->model->$name;
                        $form->model->$name = $dir . $filename;
                    }
                }
            }
            if ($form->validate()) {
                $model = $form->model;
                if ($model->save(FALSE)) {
                    foreach ($filenames as $filename) {
                        @unlink(Yii::app()->basePath . "/.." . $filename);
                    }
//                     Yii::app()->user->setFlash("success", News::model()->modelName . ( "“" . $model . "”已成功修改！" )); //遗留问题，这句执行无响应，后续代码未执行
                    $msg = Yii::app()->user->getFlashes();
                    $this->redirect(array(
                        "view",
                        "id" => $model->id
                    ));
                }
            } else {
                foreach ($images as $name) {
                    @unlink(Yii::app()->basePath . "/.." . $form->model->$name);
                }
            }
        }

        $this->render('update', array('form' => $form));
    }

}
